<?php

namespace App\Http\Controllers\admin_system;

use App\Models\V2\BookSentenceModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


class SpeakController extends Controller
{
    //言图列表
    public function index()
    {
        $data = BookSentenceModel::orderBy('updated_at', 'desc')->where('is_delete', 0)->paginate(2);
        return view('admin_system.speakImg.index', compact('data'));
    }

    //添加言图
    public function addSpeak(Request $request)
    {
        if ($request->hasFile('bg_img')) {
            $file = $request->file('bg_img');
            if ($file->isValid()) {
                $ext         = $file->getClientOriginalExtension();
                $path        = 'admin/speakImg';
                $newFileName = date('Y-m-d') . '-speak-' . uniqid() . '.' . $ext;
                $file->move($path, $newFileName);
                $data           = $request->except('_token');
                $data['bg_img'] = $path . '/' . $newFileName;
                $speakImg       = BookSentenceModel::create($data);
            }
            return redirect()->back()->with(['speakAddSuccess' => '添加成功']);
        }

        return redirect()->back()->withErrors(['speakAddError' => '添加失败']);
    }

    public function delSpeak(Request $request)
    {
        $id           = $request->route('id');
        $bookSentence = BookSentenceModel::find($id);
        if (!str_contains($bookSentence->bg_img, 'default')) {
            $path = public_path() . '/' . $bookSentence->bg_img;
            unlink($path);
        }
        $bookSentence->is_delete = 1;
        $bookSentence->bg_img    = '/uploads/guozheng/images/common/default.png';
        $status                  = $bookSentence->save();

        if ($status) {
            return redirect()->back()->with(['speakSuccess' => '删除成功']);
        }
        return redirect()->back()->withErrors(['speakError' => '删除失败了']);
    }

    public function editSpeak(Request $request)
    {
        $id   = $request->route('id');
        $data = BookSentenceModel::find($id);
        return view('admin_system.speakImg.edit', compact('data'));
    }

    public function storeSpeak(Request $request)
    {
        $id           = $request->Input('id');
        $bookSentence = BookSentenceModel::find($id);
        if ($request->hasFile('bg_img')) {
            $file = $request->file('bg_img');
            if ($file->isValid()) {//如果文件删除就删除以前的文件
                if (!str_contains($bookSentence->bg_img, 'default')) {
                    $oldPath = public_path() . '/' . $bookSentence->bg_img;
                    unlink($oldPath);//删除以前的图片
                }
                $dir         = 'admin/speakImg';
                $ext         = $file->getClientOriginalExtension();
                $newFileName = date('Y-m-d') . '-speak-' . uniqid() . '.' . $ext;
                $file->move($dir, $newFileName);
                $data           = $request->except(['id', '_token']);
                $data['bg_img'] = $dir . '/' . $newFileName;
                $status         = $bookSentence->fill($data)->save();
                return redirect()->route('speakList')->with(['speakSuccess' => '修改成功']);
            }
        } else {
            $data   = $request->except(['id', '_token']);
            $status = $bookSentence->fill($data)->save();
            return redirect()->route('speakList')->with(['speakSuccess' => '修改成功']);
        }
    }
}


















